
#import <appkit/Application.h>
#import <appkit/Cell.h>
#import <appkit/Matrix.h>
#import <appkit/PrintInfo.h>

#import "PageMargin.h"
#import "DGPrintInfo.h"

@implementation PageMargin
/*
 * PageLayout is overridden so that the user can set the margins of
 * the page.  This is a useful feature in most programs.
 *
 * The accessory view is used to add the additional fields, and
 * pickedUnits: is overridden so that the margin is displayed in the
 * currently selected units.  Note that the accessoryView is set
 * in InterfaceBuilder using the outlet mechanism!
 */


/* Get the values from the fields */
- getValues:(float *)lm right:(float *)rm top:(float *)tm bottom:(float *)bm
{
    float conversion, dummy;
    [self convertOldFactor:&conversion newFactor:&dummy];
/* Values are returned in points, just like PrintInfo */
    *lm = [leftMargin floatValue] / conversion;
    *rm = [rightMargin floatValue] / conversion;
    *tm = [topMargin floatValue] / conversion;
    *bm = [bottomMargin floatValue] / conversion;
	return self;
}

/* Set the values as passed */
- setValues:(float)lm right:(float)rm top:(float)tm bottom:(float)bm
{
    float conversion, dummy;
    [self convertOldFactor:&conversion newFactor:&dummy];
/* Values are set in Points, just like PrintInfo */
    [leftMargin setFloatValue:lm*conversion];
    [rightMargin setFloatValue:rm*conversion];
    [topMargin setFloatValue:tm*conversion];
    [bottomMargin setFloatValue:bm*conversion];
	return self;
}


- pickedUnits:sender
/*
 * Called when the user selects different units (e.g. cm or inches).
 * Must update the margin fields.
 */
{
    float old, new;

    [self convertOldFactor:&old newFactor:&new];
    [leftMargin setFloatValue:new * [leftMargin floatValue] / old];
    [rightMargin setFloatValue:new * [rightMargin floatValue] / old];
    [topMargin setFloatValue:new * [topMargin floatValue] / old];
    [bottomMargin setFloatValue:new * [bottomMargin floatValue] / old];

    return [super pickedUnits:sender];
}

- readPrintInfo
/*
 * Sets the margin fields from the Application-wide PrintInfo.
 */
{
    id pi;
    float conversion, dummy;
    NXCoord left, right, top, bottom;

    [super readPrintInfo];
    pi = [NXApp printInfo];
    [self convertOldFactor:&conversion newFactor:&dummy];
    [pi getMarginLeft:&left right:&right top:&top bottom:&bottom];
    [leftMargin setFloatValue:left * conversion];
    [rightMargin setFloatValue:right * conversion];
    [topMargin setFloatValue:top * conversion];
    [bottomMargin setFloatValue:bottom * conversion];
    return self;
}

- writePrintInfo
/*
 * Sets the margin values in the Application-wide PrintInfo from
 * the margin fields in the panel.
 */
{
    id pi;
    float conversion, dummy;

    [super writePrintInfo];
    pi = [NXApp printInfo];
    [self convertOldFactor:&conversion newFactor:&dummy];
    if (conversion) {
	[pi setMarginLeft:[leftMargin floatValue] / conversion
		    right:[rightMargin floatValue] / conversion
		      top:[topMargin floatValue] / conversion
		   bottom:[bottomMargin floatValue] / conversion];
    }

    return self;
}

/* outlet setting methods */

- setPlpAccessory:anObject
{
	plpAccessory = anObject;
	[self setAccessoryView:plpAccessory];
	[self setSideForm:[plpAccessory findViewWithTag:1]];
	[self setTopBotForm:[plpAccessory findViewWithTag:2]];
	return self;
}

- setTopBotForm:anObject
{
    [anObject setTarget:ok];
    [anObject setAction:@selector(performClick:)];
    [anObject setNextText:width];
	[anObject setFloatingPointFormat:NO left:2 right:2];
    topMargin = [anObject findCellWithTag:5];
	[topMargin setEntryType:NX_FLOATTYPE];
    bottomMargin = [anObject findCellWithTag:6];
	[bottomMargin setEntryType:NX_FLOATTYPE];
    return self;
}

- setSideForm:anObject
{
    [scale setNextText:anObject];
    [anObject setTarget:ok];
    [anObject setAction:@selector(performClick:)];
	[anObject setFloatingPointFormat:NO left:2 right:2];
    leftMargin = [anObject findCellWithTag:3];
	[leftMargin setEntryType:NX_FLOATTYPE];
    rightMargin = [anObject findCellWithTag:4];
	[rightMargin setEntryType:NX_FLOATTYPE];
    return self;
}

@end

